package cn.gson.springboot.controller;

import cn.gson.springboot.model.pojos.Customer;
import cn.gson.springboot.model.pojos.Employee;
import cn.gson.springboot.model.pojos.Opportunity;
import cn.gson.springboot.model.service.CustomerService;
import cn.gson.springboot.model.service.EmployeeService;
import cn.gson.springboot.model.service.OpportunityService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class OpportunityController {
    @Autowired
    OpportunityService oppService;
    @Autowired
    CustomerService customerService;
    @Autowired
    HttpSession session;
    @Autowired
    EmployeeService empService;

    @RequestMapping("/emp-opp-list")
    @ResponseBody
    public Map<String,Object> EmpOppList(Integer page, Integer size,Integer value,Integer num,Integer vague,String content) {
        Employee emp = (Employee) session.getAttribute("emp");
        Page<Object> obj = PageHelper.startPage(page, size);
        List<Opportunity> opp = oppService.selectEmpOpportunity(emp.getDepartment().getDeptId(),emp.getEmpId(),value,num,vague,content);
        Map<String,Object> map = new HashMap<>();
        map.put("total",obj.getTotal());
        map.put("rows",opp);
        return map;
    }

    @RequestMapping("/emp-opp-list-all")
    @ResponseBody
    public List<Opportunity> AllEmpOppList() {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Opportunity> opp = oppService.selectEmpOpportunity(emp.getDepartment().getDeptId(),emp.getEmpId(),null,null,null,null);
        return opp;
    }
    // 按客户Id查询机会
    @RequestMapping("/cus-opp-list-all")
    @ResponseBody
    public List<Opportunity> AllCusOppList(Integer cusId) {
        List<Opportunity> opp = oppService.selectCusOpportunity(cusId);
        return opp;
    }
    //计算机会员工分布
    @RequestMapping("/compute-opp-emp")
    @ResponseBody
    public List<Map<String,Object>> computeOppByEmp() {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = oppService.computeOppByEmp(emp.getDepartment().getDeptId(),emp.getEmpId());
        return map;
    }
    //计算来源分布
    @RequestMapping("/source-list")
    @ResponseBody
    public List<Map<String,Object>> sourceList() {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = oppService.computeSource(emp.getDepartment().getDeptId(),emp.getEmpId());
        return map;

    }

    //计算分类分布
    @RequestMapping("/classify-list")
    @ResponseBody
    public List<Map<String,Object>> classifyList() {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = oppService.computeClassify(emp.getDepartment().getDeptId(),emp.getEmpId());
        return map;
    }
    //计算机会发现时间月份
    @RequestMapping("/find-date-list")
    @ResponseBody
    public List<Map<String,Object>> findDateList() {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = oppService.computeFindDate(emp.getDepartment().getDeptId(),emp.getEmpId());
        return map;

    }

    //计算机会预计签单月份
    @RequestMapping("/sign-date-list")
    @ResponseBody
    public List<Map<String,Object>> signDateList() {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = oppService.computeSignDate(emp.getDepartment().getDeptId(),emp.getEmpId());
        return map;

    }

    //计算状态
    @RequestMapping("/status-list")
    @ResponseBody
    public List<Map<String,Object>> statusList() {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = oppService.computeStatus(emp.getDepartment().getDeptId(),emp.getEmpId());
        return map;
    }

    //计算用户的阶段
    @RequestMapping("/stage-list")
    @ResponseBody
    public List<Map<String,Object>> stageList() {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = oppService.computeStage(emp.getDepartment().getDeptId(),emp.getEmpId());
        return map;
    }
    //计算用户的阶段
    @RequestMapping("/stage-other-list")
    @ResponseBody
    public List<Map<String,Object>> computeStageOther() {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = oppService.computeStageOther(emp.getDepartment().getDeptId(),emp.getEmpId());
        return map;
    }

    //传入用户计算阶段
    @RequestMapping("/stage-min-list")
    @ResponseBody
    public List<Map<String,Object>> stageMinList(Integer empId) {
        List<Map<String,Object>> map = oppService.computeStageMin(empId);
        return map;
    }

    //传入用户计算全部阶段
    @RequestMapping("/stage-medium-list")
    @ResponseBody
    public List<Map<String,Object>> stageMediumList(Integer empId) {
        List<Map<String,Object>> map = oppService.computeStageMedium(empId);
        return map;
    }

    //计算全部阶段
    @RequestMapping("/stage-max-list")
    @ResponseBody
    public List<Map<String,Object>> stageMaxList() {
        List<Map<String,Object>> map = oppService.computeStageMax();
        return map;
    }

    //计算可能性
    @RequestMapping("/possibility-list")
    @ResponseBody
    public List<Map<String,Object>> possibilityList() {
        Employee emp = (Employee) session.getAttribute("emp");
        List<Map<String,Object>> map = oppService.computePossibility(emp.getDepartment().getDeptId(),emp.getEmpId());
        return map;
    }

    @RequestMapping("/save-opportunity")
    @ResponseBody
    public Opportunity saveOpportunity(@RequestBody Opportunity opportunity){
        return oppService.saveOpportunity(opportunity);
    }

    @RequestMapping("/delete-opportunity")
    @ResponseBody
    public void deleteOpportunity(@RequestParam(value = "oppId") Integer oppId){
        System.out.println(oppId);
        oppService.deleteOpportunity(oppId);
    }

}
